import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import org.apache.poi.hssf.record.formula.functions.MathX;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import br.uff.gdse.positionevaluation.entity.Result;
import br.uff.gdse.positionevaluation.evaluators.ExistInterface;
import br.uff.gdse.positionevaluation.evaluators.SednaInterface;
import br.uff.gdse.positionevaluation.evaluators.XIndiceInterface;
import br.uff.gdse.positionevaluation.evaluators.XMLDBEvaluator;
import br.uff.gdse.positionevaluation.evaluators.XQueryProcessorEvaluator;
import br.uff.gdse.positionevaluation.queries.XMLQuery;
import br.uff.gdse.positionevaluation.util.DateUtil;
import br.uff.gdse.positionevaluation.util.ExcelUtil;

public class RunEvaluations {

	private static final String OUTPUT_FOLDER = "C:\\Desenvolvimento\\workspaces\\workGDSE\\output\\";
	
	public static void main(String[] args) {
		runSednaEvaluation();
	}
	
	private static void runXIndiceEvaluation() {
		XMLDBEvaluator evaluator = new XMLDBEvaluator(new XIndiceInterface());
		List<Result> results = evaluator.evaluate(null);
		
		writeResultsOnXLS(results, "XIndice");
	}
	
	private static void runExistEvaluation() {
		XMLDBEvaluator evaluator = new XMLDBEvaluator(new ExistInterface());
		XMLQuery q = new XMLQuery("/db", "/dblp/article");
		List<Result> results = evaluator.evaluate(q);
		
		writeResultsOnXLS(results, "Exist");
	}
	
	private static void runSednaEvaluation() {
		XMLDBEvaluator evaluator = new XMLDBEvaluator(new SednaInterface());
		XMLQuery q = new XMLQuery("", "doc('dblp')/dblp/article");
		List<Result> results = evaluator.evaluate(q);
		
		writeResultsOnXLS(results, "Sedna");
	}
	
	private static void runGalaxEvaluation() {
		XQueryProcessorEvaluator evaluator = new XQueryProcessorEvaluator();
		List<Result> results = evaluator.evaluate("galax-run -context-item /home/luiz/galax/XMark0.5.xml ", "/home/luiz/galax/xmq", ".xq");
		
		writeResultsOnXLS(results, "Galax");
	}

	private static void writeResultsOnXLS(List<Result> results, String dbName) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet();
		int rowIndex = ExcelUtil.createEvaluationHeader(sheet, dbName);
		
		for (Result result : results) {
			HSSFRow row = sheet.createRow(rowIndex++);
			row.createCell(0).setCellValue(result.getXMLQuery().getQuery());
			row.createCell(1).setCellValue(MathX.average(result.getResponseTimesExceptFirst()));
		}
		
		try {
			FileOutputStream fos = new FileOutputStream(OUTPUT_FOLDER + dbName + "_" + DateUtil.format(Calendar.getInstance().getTime()) + ".xls");
			wb.write(fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
